
package com.tools.cleanmaster.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.view.ToastHelper;

/**
 * booster带量
 *
 * @author lianzhan
 */
public class Recommend {
    public static final int MAIN = 1;
    public static final int BOOSTER = 1;
    public static final int BATTERY = 2;
    public static final int ES_FILE = 3;

    public static final String CARD = "card";
    public static final String MEMORY_HIGH = "memory_high";
    public static final String SHARP = "sharp";
    public static final String MENU = "menu";

    private static boolean isInstall(Context context, String pkg) {
        boolean isAppInstalled = PackageUtils.isPkgInstalled(context, pkg);
        if (isAppInstalled) {
            installForPkg(context, pkg);
        }
        return isAppInstalled;
    }

    private static void installForPkg(Context context, String pkg) {
        try {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(
                    pkg);
            if (intent != null) {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                intent.putExtra(CleanConstant.EXTRA_FROM_KEY,
                        CleanConstant.EXTRA_FROM_VALUE_CLEAN_MASTER);
                context.startActivity(intent);
            }
        } catch (Exception e) {
        }
    }

    /*public static boolean isInstallBooster(Context context) {
        return isInstall(context, CleanConstant.PKG_NAME_BOOSTER);
    }*/

    /*private static boolean isInstallBattery(Context context) {
        return isInstall(context, CleanConstant.PKG_NAME_BATTERY);
    }*/

    public static void startBooster(Context context, String entranceType) {
        /*if (!isInstallBooster(context)) {
            if (!NetworkUtils.isNetworkAvaialble(context)) {
                ToastHelper.create(context, R.string.recommend_network_message, Toast.LENGTH_LONG).show();
            } else {
                if (CARD.equals(entranceType)) {
                    startGooglePlay(context, CleanConstant.CARD_BOOSTER_PLAY_URL);
                } else if (MEMORY_HIGH.equals(entranceType)) {
                    startGooglePlay(context, CleanConstant.MEMORY_BOOSTER_PLAY_URL);
                } else if (MENU.equals(entranceType)) {
                    startGooglePlay(context, CleanConstant.MENU_BOOSTER_PLAY_URL);
                }
            }
        }
        GlobalConfigsMgr.setRecommendBoosterTime(CleanApplication.getInstance(), System.currentTimeMillis());*/
    }

    private static void startGooglePlay(Context context, String url) {
        Intent playIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        playIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        playIntent.setPackage(CleanConstant.PLAY_PACKAGE_NAME);
        try {
            context.startActivity(playIntent);
        } catch (Exception e) {
            // no play crash
            startBrowser(context, url);
        }
    }

    protected static void startBrowser(Context context, String url) {
        if (url == null) {
            ToastHelper.create(context, R.string.recommend_network_message, Toast.LENGTH_LONG).show();
            return;
        }
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            ToastHelper.create(context, R.string.recommend_no_browser_play, Toast.LENGTH_LONG).show();
        }
    }


    public static void startBattery(Context context, String entranceType) {
        /*if (!isInstallBattery(context)) {
            if (!NetworkUtils.isNetworkAvaialble(context)) {
                ToastHelper.create(context, R.string.recommend_network_message, Toast.LENGTH_LONG)
                        .show();
            } else {
                if (CARD.equals(entranceType)) {
                    startGooglePlay(context, CleanConstant.CARD_BATTERY_PLAY_URL);
                } else if (SHARP.equals(entranceType)) {
                    startGooglePlay(context, CleanConstant.SHARP_BATTERY_PLAY_URL);
                }
            }
        }
        GlobalConfigsMgr.setRecommendBatteryTime(CleanApplication.getInstance(), System.currentTimeMillis());*/
    }



    public static void downLoadRecommandApp(Context mContext, String pkgName) {
        String downLoadUrl = "";
        //TODO 下载推荐应用
        startGooglePlay(mContext, downLoadUrl);
    }
}
